package sw6.launcher;

import sw6.launcher.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class SSIDBlack extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.ssidblack);
                Uri dataUri = getIntent().getData();
                
                if(dataUri == null)
                	finish();

                ListView strings = (ListView) findViewById(R.id.Stringlistblack);  
                Button newstring = (Button) findViewById(R.id.newitemblack);
                strings.setAdapter(new SSIDAdapter(dataUri, this));

                newstring.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                final ListView strings = (ListView) findViewById(R.id.Stringlistblack);
                                AlertDialog.Builder alert = new AlertDialog.Builder(SSIDBlack.this);

                                alert.setTitle("Add New SSID");
                                alert.setMessage("Type the name of the SSID. The SSID is case-sensitive!");

                                final EditText input = new EditText(SSIDBlack.this);
                                alert.setView(input);
                                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int whichButton) {
                                                String value = input.getText().toString();
                                                ((SSIDAdapter)strings.getAdapter()).additem(value);
                                        }
                                });
                                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int whichButton) {
                                        }
                                });
                                alert.show();
                        }
                });

                strings.setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View strings, int position,
                                        long id) {
                                final ListView stringslist = (ListView) findViewById(R.id.Stringlistblack);
                                AlertDialog.Builder alert = new AlertDialog.Builder(SSIDBlack.this);
                                final int pos = position;

                                alert.setTitle("Edit SSID");
                                alert.setMessage("Type the new name of the SSID. The SSID is case-sensitive!");

                                final EditText input = new EditText(SSIDBlack.this);
                                input.setText((String)((SSIDAdapter)stringslist.getAdapter()).getItem(position));
                                alert.setView(input);
                                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int whichButton) {
                                                String value = input.getText().toString();
                                                ((SSIDAdapter)stringslist.getAdapter()).edititem(pos, value);
                                        }
                                });
                                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int whichButton) {
                                        }
                                });
                                alert.show();
                        }
                });

                strings.setOnItemLongClickListener(new OnItemLongClickListener() {
                        @Override
                        public boolean onItemLongClick(AdapterView<?> parent, View strings,
                                        int position, long id) {
                                final ListView stringslist = (ListView) findViewById(R.id.Stringlistblack);
                                AlertDialog.Builder alert = new AlertDialog.Builder(SSIDBlack.this);
                                final int pos = position;

                                alert.setTitle("Delete SSID");
                                alert.setMessage("Do you want to delete this particular SSID?");

                                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int whichButton) {
                                                ((SSIDAdapter)stringslist.getAdapter()).deleteitem(pos);
                                        }
                                });
                                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int whichButton) {
                                        }
                                });
                                alert.show();
                                return true;
                        }
                });  
        }
}